package com.rc.saas.tenant.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.MessageFormat;

/**
 * 生成中电联二维码链接
 * 格式：hlht://19000391.914401153/
 * Created by sven on 2018-05-29
 */
public class QrCodeUtils {
    private static Logger logger = LoggerFactory.getLogger(QrCodeUtils.class);
    //互联互通枪号二维码前缀
    private static String prefix = "hlht://";
    // 运营商标识（OperatorID）/组织机构代码(企业名称，营业执照)
    //private static String OperatorID = ".914401153/";
    private static final String OperatorIdTemp = ".{0}/";

    private static String wechat_qcode_expression = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx00a6fa0a81e19fe9&redirect_uri=https://saastest.jin-zhuang.cn/jinzhuang-saas-wechat/templates/charge/detail.jsp?gunNo=19001401&elecStationId=900&response_type=code&scope=snsapi_base&state=123#wechat_redirect";


    //瑞晨小程序二维码
    private static final String miniMessageFormat = "https://charge.ruishangkj.com/rc-mini/open/qrcode/{0}";

    /**
     * 生成中电联二维码链接
     */
    public static String createCecQRCode(String tenantCode, String epNo) {
        logger.info("QrCodeUtils.createCecQRCode:{}", epNo);
        tenantCode = MessageFormat.format(OperatorIdTemp, tenantCode);

        if (StringUtils.isNotBlank(epNo)) {
            String qrcode = prefix + epNo + tenantCode;

            return qrcode;
        } else {
            return null;
        }
    }

    /**
     * 生成小程序二维码链接
     */
    public static String createMiniQRCode(String tenantCode, String epNo) {
        logger.info("createMiniQRCode.createCecQRCode:{}", epNo);
        String qrcode = MessageFormat.format(miniMessageFormat, epNo);
        if (StringUtils.isNotBlank(qrcode)) {
            return qrcode;
        } else {
            return null;
        }
    }

    //微信公众号网页授权
    public static String createWechatQRCode(String tenantCode, String epNo, String elecStationId) {
        logger.info("QrCodeUtils.createWechatQRCode:{},{}", epNo, elecStationId);
        String qrcode = null;
        if (!StringUtils.isAnyEmpty(epNo, elecStationId)) {
            qrcode = MessageFormat.format(wechat_qcode_expression, epNo, elecStationId);
            return qrcode;
        } else {
            return qrcode;
        }
    }

    /**
     * 生成中电联带自定义内容的扩展版二维码链接
     */
    public static String createCecQRCodeExt(String tenantCode, String epNo, String masterTenantCode) {
        logger.info("QrCodeUtils.createCecQRCodeExt:{}", epNo);
        tenantCode = MessageFormat.format(OperatorIdTemp, tenantCode);

        if (StringUtils.isNotBlank(epNo)) {
            String qrcode = prefix + epNo + tenantCode + masterTenantCode;

            return qrcode;
        } else {
            return null;
        }
    }

    /**
     * 从中电联二维码链接返回枪号
     */
    public static String getGunNoFromCecQrCode(String tenantCode, String scanQRCode) {
        logger.info("QrCodeUtils.getEpnoFromCecQrCode:{}", scanQRCode);

        if (StringUtils.isBlank(scanQRCode)) {
            return null;
        }
        tenantCode = MessageFormat.format(OperatorIdTemp, tenantCode);

        String startToken = prefix;
        String endToken = tenantCode;

        return StringUtils.substringBetween(scanQRCode, startToken, endToken);
    }

    public static void main(String[] args) {
        //String qrcode = createCecQRCode("MA01H3BQ1", "19000391");
        //System.out.println("createCecQRCode:" + qrcode);
        //System.out.println("getEpnoFromCecQrCode:" + getGunNoFromCecQrCode("MA01H3BQ1", qrcode));
        //
        ////扩展版
        //qrcode = createCecQRCodeExt("MA01H3BQ1", "19000391", "914401153");
        //System.out.println("createCecQRCodeExt:" + qrcode);
        //System.out.println("getEpnoFromCecQrCode:" + getGunNoFromCecQrCode("MA01H3BQ1", qrcode));

        String wechatQRCode = QrCodeUtils.createWechatQRCode("123", "19001401", "900");
        System.out.println(wechatQRCode);
        //String miniQRCode = createMiniQRCode("19001401", "19001401");
    }
}
